*** Heterogeneity main regressions
use "${PilotPublicData}/hgopy_pilot_public.dta", clear
*parameters 
eststo clear
local outcomes = "adopted_larc" 
local groups = " depgm ado single"
*loop over outcomes
foreach y of local outcomes {
	*loop over groups
	foreach g of local groups {
		*controls
		local controls = " "
		local model=""
		foreach c of local controls {
			capture drop `c'_cen
			qui su `c', meanonly
			gen `c'_cen = `c' - r(mean)
			local varlab : var label `c' 
			label var `c'_cen "`varlab'"
			local model = "`model' b0.pd##c.`c'_cen"
		}
 
		capture matrix drop M 
		*regress for groups, but show the output
		gen group = `g'
		noisily di "Group var: `g'"
		noisily reg `y' 		b0.pd##group		`model' , robust coefleg
		*collect coefs
		lincom 1.pd 
		local b0 = r(estimate)
		local se0 = r(se)
		matrix input M = ( `b0' \ `se0' )
		*group coefs
		lincom 1.pd#1.group + 1.pd
		local b0 = r(estimate)
		local se0 = r(se)
		matrix M = nullmat(M) , ( `b0' \ `se0' )
		*constants, or control  means
		noisily su `y' if control==1 & group==0, meanonly
		local c0 = r(mean)
		noisily su `y' if control==1 & group==1, meanonly
		local c1 = r(mean)
		matrix M = nullmat(M) \ ( `c0' , `c1' )
		*tests
		test 1.pd==0.pd   // Full = Discount within group
		local p0=r(p)
		test 1.pd#1.group + 1.pd == 0.pd#1.group + 0.pd // Full = Discount within groups
		local p1=r(p)
		test 1.pd#1.group // Discount = Discount between groups
		local p3=r(p)
		matrix M = nullmat(M) \ ( `p0' , `p1' \ `p3', . )
    mat list M
		*get N
		su `y' if group==0
		local n0 = r(N)
		su `y' if group==1
		local n1 = r(N)
		local n = e(N)
		matrix M = nullmat(M) \ ( `n0' , `n1' \ `n' , . )
		*show results
		matrix list M
		matrix `g' = nullmat(`g') , M
		*prep next run
		drop group 
	}
	*turn into strings
	qui foreach g of local groups {
		matrix list `g'
		svmat `g'
		*stars and SEs cleaning up
		forvalues n=1/2 {
			gen s`n'=""
			/* no stars actually
			replace s`n'=s`n'+"*" if _n==1 & abs(`g'`n'[1] / `g'`n'[2])>1.282
			replace s`n'=s`n'+"*" if _n==1 & abs(`g'`n'[1] / `g'`n'[2])>1.645
			replace s`n'=s`n'+"*" if _n==1 & abs(`g'`n'[1] / `g'`n'[2])>2.326	
			replace s`n'=s`n'+"*" if _n==3 & abs(`g'`n'[3] / `g'`n'[4])>1.282
			replace s`n'=s`n'+"*" if _n==3 & abs(`g'`n'[3] / `g'`n'[4])>1.645
			replace s`n'=s`n'+"*" if _n==3 & abs(`g'`n'[3] / `g'`n'[4])>2.326
			*/
			gen X`g'`n'=string(`g'`n',"%11.3f")+s`n'
			replace X`g'`n'="("+X`g'`n'+")" if inlist(_n,2)
			replace X`g'`n'="" if X`g'`n'=="."
			drop s`n' `g'`n' 
		}
		matrix drop `g' 
	}
	*export
	if ("`y'"=="adopted_larc") local yvar = "LARC"
	*tempfile for a table 
	preserve 
	keep X* 
	drop if Xdepgm1==""
	tempfile tab_`y'
	save `tab_`y'', replace
	restore
	*excel tables
// 	noisily export excel X* using "${Out}/T-Heterogeneity-Discount.xlsx" , sheetmodify sheet("Main - `yvar'") cell(B4) keepcellfmt
	drop X* 
} 

***Export as a nice latex  file
*use tempfiles
use `tab_adopted_larc', clear
// append using `tab_adopted_none'

*create a row counter
gen n=_n
*drop mid rows 
// drop if inlist(_n,6,7)

*local with first column names 
gen A=""
order A, first 
#delimit ;
local firstcol = `" "LARC price: Discounted" " " "Control mean w/n group" "Discount=Full w/n group (p-val.)" "Discount=Discount b/w groups (p-val.)" 
					  "Obs. in group" "Obs." "' ;
#delimit cr 
local i=0
foreach j of local firstcol {
	local ++i 
	replace A="`j'" if _n==`i'
}
*remove triple zeroes
ds A n, not
foreach v in `r(varlist)' {
	replace `v' = subinstr(`v',".000","",.) 
} 
*add & signs
ds A n, not
foreach v in `r(varlist)' {
	replace `v' = "& " + `v' if `v'!=""
} 

*combine multirows 
foreach v in `r(varlist)' {
	replace `v' = subinstr(`v'," "," \multicolumn{2}{c}{",1) if `v'!="" & inlist(_n,5,7)
	replace `v' = `v' + "}" if `v'!="" & inlist(_n,5,7)
} 

*combine into one row
drop n
gen X=A + Xdepgm1 + Xdepgm2 + Xado1 + Xado2 + Xsingle1 + Xsingle2
keep X
*export 
#delimit ;
texsave_custom using "${OutTexTab}/TabS9-impact-het-main-discount.tex", 
replace nonames nofix hlines(3 4 5 6)  frag
	align("p{0.37\textwidth}*{6}{c}")
	headerlines("  & (1) & (2) & (3) & (4) & (5) & (6) " 
				" & \multicolumn{2}{c}{\textbf{Department}} & \multicolumn{2}{c}{\textbf{Age group}}  & \multicolumn{2}{c}{\textbf{Marital status}}  "
				" \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} & FP & Mat./Gyn. & Age$\geq$20 & Age<20 & Mar./Coh. & Single ") ;
#delimit cr
